﻿using oceanho.webapi.factory.httpAttr;
using oceanho.webapi.factory.httpCore;
using oceanho.webapi.factory.httpEntity;
using oceanho.webapi.factory.httpHandler;
using System;
using System.Collections.Generic;
using System.Web;

namespace oceanho.webapi.factory.Tests.serviceHandlers
{
    /// <summary>
    /// 文件上传HttpHandler Service
    /// </summary>
    public class FileUpload_ServiceHandler : AppBase_ServeiceHandler
    {
        protected override void Upload(UploadParameter arg)
        {
            arg.UnBindEvent();

            // 添加 或者
            // 删除自定义允许上传文件类型
            //arg.UnOrAllowTypes.Add("chm");
            //arg.UnOrAllowTypes.Add("ini");
            //arg.UnOrAllowTypes.Add("css");

            // 重命名上传文件保存名称
            //foreach (UploadItem item in arg.FileUploads)
            //{
            //    item.FileName = Guid.NewGuid() + "." + item.FileSuffix;
            //}

            // 上传文件类型            
            arg.FileTypes.Clear();
            arg.FileTypes.Add("exe"); // 禁止上传 exe 
            arg.TypeValidMode = FileUploadTypeValidMode.UnAllowType;

            // 上传事件订阅
            arg.OnUploadItemStart += Arg_OnUploadItemStart;
            arg.OnUploadItemCompleted += Arg_OnUploadItemCompleted;
            
            arg.OnUploadItemValidFailed += Arg_OnUploadItemValidFailed;
            arg.OnUploadItemFailedHandler += Arg_OnUploadItemFailedHandler;
            arg.OnUploadItemProgressChange += Arg_OnUploadItemProgressChange;

            // 解除绑定事件委托函数列表
            // arg.UnBindEvent();
            base.Upload(arg);
        }

        private bool Arg_OnUploadItemValidFailed(UploadItem upload, FileUploadTypeValidOption validOption)
        {
            base.Html("调用 -> OnUploadItemValidFailed {0} ,Suffix is {1}<br/>", upload.FileName, upload.FileSuffix);
            return false;
            // throw new NotImplementedException();
        }

        private void Arg_OnUploadItemStart(UploadItem upload)
        {
            base.Html("调用 -> OnUploadItemStart {0} <br/>", upload.FileName);
            // throw new NotImplementedException();
        }

        void Arg_OnUploadItemCompleted(UploadItem upload)
        {
            base.Html("调用 -> OnUploadItemCompleted {0} <br/>", upload.FileName);
            // throw new NotImplementedException();
        }
        void Arg_OnUploadItemFailedHandler(UploadItem upload, Exception fail)
        {
            base.Html("调用 -> OnUploadItemFailedHandler {0} <br/> Exception{1}", upload.FileName, fail.ToString());
        }
        void Arg_OnUploadItemProgressChange(UploadItem upload, long compelted)
        {
            base.Html("调用 -> OnUploadItemProgressChange {0},progress is {1} <br/>", upload.FileName, compelted);
        }

        /// <summary>
        /// 路由标记（CallRouterAttribute标记类）
        /// </summary>
        [Router("data/service/upload/pic")]
        public void UploadUserPic()
        {
            base.Content("router is {0}, method is {1}", " data/service/upload/pic", "UploadUserPic");
        }
    }
}